import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class WebView extends StatefulWidget {
  final title;
  final url;

  const WebView({Key key, this.url, this.title}) : super(key: key);

  @override
  _WebViewState createState() => _WebViewState();
}

class _WebViewState extends State<WebView> {
  String _title, _url;
  TextEditingController controller = TextEditingController();
  FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();

  @override
  void initState() {
    super.initState();
    flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged wvs) {
      print('wvs.type:${wvs.type}');
    });

    var _listTitle = List<int>();
    jsonDecode(widget.title).forEach(_listTitle.add);
    _title = Utf8Decoder().convert(_listTitle);

    var _listUrl = List<int>();
    jsonDecode(widget.url).forEach(_listUrl.add);
    _url = Utf8Decoder().convert(_listUrl);

    print('_title:$_title,_url:$_url');
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff23AFDC),
        elevation: 0,
        title: Text(_title),
        centerTitle: true,
      ),
      url: _url,
      geolocationEnabled: true,
      allowFileURLs: true,
      appCacheEnabled: true,
      withZoom: false,
    );
  }
}
